<template>
  <orh-table add="add" del="del" @on-del="confirm" pages @on-add="addSilde" :pageDate="pageDate" @on-page-change="queryData" reload @on-reload="refresh">
    <Table :data="DataList" :columns="columns1" @on-selection-change="selectOne" :loading="tableload" slot="table"></Table>
	<orh-sidebar v-model="show" :title="layouttitle" scale slot="others">
        <Operation :infoId="infoId" @requery="requery"></Operation>
    </orh-sidebar>
  </orh-table>

</template>

<script>
  export default {
    components: {
		Operation: resolve => {require(['./Operation.vue'], resolve);},
	},
	name: 'leave-leaverules',
    data () {
      return {
		infoId: null,
		layouttitle:null,
        DataList:[],
        tableload: true,
		del:[],
        pageDate:{
			total:0,
			current_page:1,
			per_page:0
		},
		delUrl:'api/leave/v1/assessment_leave/delete/',
        show:false,
        columns1: [
			{
				type: 'selection',
				width: 60,
				align: 'center',
			},
			{
				title: '类型',
				align: 'center',
				render: (h, params) => {
					return h('div', params.row.type == 0?'病假':'事假');
				}
			},
			{
				title: '几天不扣分',
				align: 'center',
				render: (h, params) => {
					return h('div', params.row.num);
				}
			},
			{
				title: '超出每天扣多少分',
				align: 'center',
				render: (h, params) => {
					return h('div', params.row.score);
				}
			},
			{
				title: '操作',
				key: 'operation',
				width: 200,
				align: 'center',
				render: (h, params) => {
					return h('div', [
					h('Button', {
						directives: [
						{
							name: 'button',
							value: 'edit',
						}
					],
					props: {
						type: 'primary',
						size: 'small'
					},
					style: {
						marginRight: '5px',
					},
					on: {
						click: () => {
						this.update(params.row.id)
						}
					}
					}, '编辑'),
					h('Button', {
						directives: [
							{
								name: "button",
								value: "del"
							}
						],
						props: {
							type: 'error',
							size: 'small'
						},
						style: {
							marginRight: '5px',
						},
						on: {
							click: () => {
								this.del = [params.row.id]
								this.confirm()
							}
						}
					}, '删除'),
				]);
				}
			}
			],
		}
    },
    methods: {
		/************************************************************
         * 获取数据
         ************************************************************/
		queryData(index){
			this.tableload = true
			this.$http.get(`api/leave/v1/assessment_leave/index?page=${index}`).then(val=>{
				this.DataList=val.data.data
				if(val.pages!=null){
					this.pageDate.total=val.data.total
					this.pageDate.per_page=val.data.per_page
					this.pageDate.current_page=val.data.current_page
				}
				this.tableload = false
			}).catch(err => {
				if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
				this.tableload = false
			})
		},
		/************************************************************
         * 添加
         ************************************************************/
		addSilde(){
			this.show = true
			this.layouttitle='添加'
			this.infoId = null
		},
		/************************************************************
         * 修改
         ************************************************************/
		update(index){
			this.show = true
			this.infoId = index
			this.layouttitle='修改'
		},
		/************************************************************
         * 重新请求数据
         ************************************************************/
		requery(val){
			if(val == true){
				this.show = false
				this.queryData(this.pageDate.current_page)
			}
		},
		/************************************************************
         * 批量删除
         ************************************************************/
        selectOne(index){
			this.del = index.map(item => item.id)
        },
        /************************************************************
         * 执行删除操作
         ************************************************************/
        confirm () {
            this.$Modal.confirm({
                title: '数据删除',
                content: '<p>您确定要删除这些数据吗?</p>',
                onOk: () => {
                    if(this.del.length==0){
                        this.$Message.error('请选择删除项');
                        return
                    }
                    this.del = JSON.stringify(this.del)
                    this.tableload = true
                    this.$http.delete(`${this.delUrl}${this.del}`).then(val=>{
                        this.del=[]
                        this.queryData(this.pageDate.current_page)
                        this.$Message.success(val.message);
                    }).catch(err=>{
                        if(err.code && err.code==422) {
                            this.$Message.error(err.message);
                        }
                        this.del=[]
                        this.tableload = false
                    })
                },
            });
        },
		/************************************************************
         * 执行刷新操作
         ************************************************************/
		refresh () {
			this.queryData(1)
		},
    },
    mounted(){
      	this.queryData(this.pageDate.current_page)
    }
}
</script>



